<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>题目管理</title>
    <link rel="stylesheet" type="text/css" href="layui/css/layui.css">
    <style type="text/css">
        .layui-table-cell {
            height: 100%;
            max-width: 100%;
        }
    </style>
</head>
<body>

<form class="layui-form">
    <button type="button" class="layui-btn" id="batchDeleteBtn">
        <i class="layui-icon">&#xe640;</i> 批量删除
    </button>
    <button type="button" class="layui-btn" id="addQuestionBtn">
        <i class="layui-icon">&#xe608;</i> 添加题目
    </button>

    <div class="layui-inline" style="margin-left: 620px">
        <div class="layui-input-inline">
            <input type="text" id="value" style="width: 330px" name="value" lay-verify="required" placeholder="请输入题目名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <button type="button" class="layui-btn" id="search">搜索</button>
</form>

<table id="questionTable" lay-filter="questionTable"></table>


<!-- 添加页面开始 -->
<form class="layui-form" id="addQuestionForm" lay-filter="addQuestionForm" style="display: none;padding: 20px 55px 0px 0px">
    <div class="layui-form-item">
        <label class="layui-form-label">题目名称</label>
        <div class="layui-input-block">
            <input type="text"  name="questionTitle" lay-verify="required" placeholder="请输入题目名称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">题目类型</label>
        <div class="layui-input-block">
            <input type="text" name="questionType" lay-verify="required" value="单选" readonly class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">积分值</label>
        <div class="layui-input-block">
            <input type="number" name="integralValue" lay-verify="required" placeholder="请输入积分值" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <input type="text"  name="optionA" lay-verify="required" placeholder="请输入选项A" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <input type="text"  name="optionB" lay-verify="required" placeholder="请输入选项B" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <input type="text"  name="optionC" lay-verify="required" placeholder="请输入选项C" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <input type="text"  name="optionD" lay-verify="required" placeholder="请输入选项D" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit  lay-filter="addQuestionSubmit">立即添加</button>
            <button type="button" id="closeQuestionAdd" class="layui-btn layui-btn-primary">关闭</button>
        </div>
    </div>
</form>
<!-- 添加页面结束 -->

<!-- 编辑页面开始 -->
<form class="layui-form" id="amendQuestionForm" lay-filter="amendQuestionForm" style="display: none;padding: 20px 55px 0px 0px">
    <input type="hidden" id="id" name="id" value="">
    <div class="layui-form-item">
        <label class="layui-form-label">题目名称</label>
        <div class="layui-input-block">
            <input type="text"  name="questionTitle" lay-verify="required"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">题目类型</label>
        <div class="layui-input-block">
            <input type="text" name="questionType" lay-verify="required" value="单选" readonly class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">积分值</label>
        <div class="layui-input-block">
            <input type="number" name="integralValue" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <input type="text"  name="optionA" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <input type="text"  name="optionB" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <input type="text"  name="optionC" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <input type="text"  name="optionD" lay-verify="required" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit  lay-filter="amendQuestionSubmit">确认修改</button>
            <button type="button"  id="closeAmendQuestion" class="layui-btn layui-btn-primary">关闭</button>
        </div>
    </div>
</form>
<!-- 编辑页面结束 -->



<script type="text/javascript" src="layui/layui.js"></script>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">


    layui.use(['table','form','layer'],function () {

        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        table.render({
            elem:'#questionTable'
            ,url:'question/list'
            ,page:true
            ,limit:6
            ,limits:[4,6,8,10]
            ,cols:[[
                {type:'checkbox'}
                ,{field:'id',title:'编号',sort:true}
                ,{field:'questionTitle',title:'题目名称'}
                ,{field:'questionType',title:'题目类型'}
                ,{field:'integralValue',title:'积分值',  sort:true}
                ,{field:'optionA',title:'选项A'}
                ,{field:'optionB',title:'选项B'}
                ,{field:'optionC',title:'选项C'}
                ,{field:'optionD',title:'选项D'}
                ,{toolbar: '#barDemo', align:'center'}
            ]]
        });
        /**
         * 监听questionTable的查看编辑删除按钮的单击事件
         * */
        table.on('tool(questionTable)',function (obj) {
            var data = obj.data;
            //编辑按钮的单击事件
            if(obj.event=='edit'){
                form.val("amendQuestionForm",data);
                layer.open({
                    type:1,
                    title:['编辑题目信息','font-size:18px;color:orange;'],
                    area:['760px'],
                    content:$("#amendQuestionForm"),
                });
            }
            //删除按钮的单击事件
            if(obj.event=='del'){
                layer.confirm("你确定要删除吗",{icon:'3',title:'提示'},function () {
                    $.ajax({
                        url:'question/delete',
                        data:{id:data.id},
                        type:"delete",
                        dataType:"json",
                        success:function (result) {
                            if(result.code==200){
                                table.reload("questionTable",{
                                    page:{
                                        curr:1
                                    }
                                });
                                layer.msg("删除题目成功！",{icon:'1',title:'提示'});
                            }
                        }
                    });
                })
            }
        });
        /**
         * 点击添加按钮弹出层展示添加页面
         * */
        $("#addQuestionBtn").click(function () {
            layer.open({
                type:1,
                area:['760px'],
                title:['添加题目','font-size:18px;color:orange;'],
                content:$("#addQuestionForm"),
            });
        });
        /**
         * 监听添加用户页面的 立即添加 按钮
         * */
        form.on("submit(addQuestionSubmit)",function (obj) {
            layer.confirm("你确定要添加吗？",{icon:'3',title:'提示'},function () {
                $.ajax({
                    url:'question/insert',
                    data:obj.field,
                    type:"post",
                    dataType:"json",
                    success:function (data) {
                        if(data.code==200){
                            obj.form.reset();
                            layer.closeAll();
                            table.reload("questionTable");
                            layer.msg("添加题目成功！",{icon:'1',title:'提示'});
                        }
                    }
                });
            });
            return false;
        });

        /**
         * 监听修改页面的确认修改按钮
         * */
        form.on("submit(amendQuestionSubmit)",function (obj) {
            layer.confirm("你确定要修改吗？",{icon:'3',title:'提示'},function () {
                $.ajax({
                    url:'question/update',
                    data:obj.field,
                    type:"put",
                    dataType:"json",
                    success:function (data) {
                        if(data.code==200){
                            obj.form.reset();
                            layer.closeAll();
                            table.reload("questionTable");
                            layer.msg("修改题目成功！",{icon:'1',title:'提示'});
                        }
                    }
                });
            })
            return false;
        });
        /**
         * 各个页面的一些关闭按钮
         * */
        $("#closeQuestionAdd").click(function () {
            layer.closeAll();
        });
        $("#closeAmendQuestion").click(function () {
            layer.closeAll();
        })
        /**
         * 搜索功能
         * */
        $("#search").click(function () {
            if($("#value").val()==""){
                table.reload("questionTable",{
                    url:'question/list',
                    page:{
                        curr:1
                    }
                });
            }else{
                table.reload("questionTable",{
                    url:'question/search',
                    where:{
                        questionTitle:$("#value").val()
                    }
                });
            }
        });

        /**
         *批量删除功能
         * */
        $("#batchDeleteBtn").click(function () {
            var checkStatus = table.checkStatus('questionTable');
            if(checkStatus.data.length>0){
                layer.confirm("你确定要删除吗？",{icon:'3',title:'提示'},function () {
                    var ids = "";
                    for(var i=0;i<checkStatus.data.length;i++){
                        ids += "ids="+checkStatus.data[i].id+"&"
                    }
                    $.ajax({
                        url:"question/batchDelete",
                        data:ids,
                        type:"delete",
                        dataType:"json",
                        success:function (data) {
                            if(data.code==200){
                                table.reload("questionTable",{
                                    page:{
                                        curr:1
                                    }
                                });
                                layer.msg("批量删除题目成功！",{icon:'1',title:'提示'});
                            }
                        }
                    });
                })
            }else{
                layer.msg("请至少选择一个题目！",{icon:'2',title:'提示'});
            }
        });






    });
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

</body>
</html>